#include "viewer-file.h"

#ifdef DERIVABLE
    #include "viewer-audio-file.h"
#endif


#if defined(INF_EDITABLE) || defined(INF_LOSSY)
    #include "viewer-editable.h"
#endif

#ifdef INF_LOSSY
    #include "viewer-editable-lossy.h"
#endif

static void file_saved(GObject *obj, gpointer data) {
    g_print("calling %s\n", __func__);    
}

static void file_changed(GObject *obj, const char *content, gpointer data) {
    const char *filename = NULL;
    g_object_get(obj, "filename", &filename, NULL);
    g_print("file %s changed, new content: %s\n", filename, content);
}

static void property_changed (gpointer   obj,
         gpointer     arg_1,
         gpointer     user_data)
{
    GParamSpec     *pspec = (GParamSpec*) arg_1;
    GValue new_val = {0};
    const char *val_content = NULL;
    const char *name = g_param_spec_get_name(pspec);

    g_print("calling %s\n", __func__);    
 
    g_value_init(&new_val, G_PARAM_SPEC_VALUE_TYPE(pspec));
    g_object_get_property(obj, name, &new_val);
    val_content = g_strdup_value_contents(&new_val);
    g_print("property %s changed, new value: %s\n", name, val_content);
    g_free((gpointer)val_content);
    return;
}


static void zoom_changed (gpointer   obj,
         gpointer     arg_1,
         gpointer     user_data)
{
    guint zoom_level;

    g_print("calling %s\n", __func__);    

    g_object_get(obj, "zoom-level", &zoom_level, NULL);
    g_print("property zoom-level changed, new value: %u, user_data: addr: %p\n", 
            zoom_level,
            user_data);
    return;
}
int main(void) {
    GValue val = G_VALUE_INIT;
    gchar buf[] = "BUFFER";
#ifdef DERIVABLE
    ViewerFile *vf = g_object_new(VIEWER_TYPE_AUDIO_FILE, NULL); //子类转父类
#else
    ViewerFile *vf = g_object_new(VIEWER_TYPE_FILE, NULL);
#endif

    g_signal_connect(vf, "changed", G_CALLBACK(file_changed), NULL);
    g_signal_connect(vf, "notify", G_CALLBACK(property_changed), NULL);
    g_signal_connect(vf, "notify::zoom-level", G_CALLBACK(zoom_changed), NULL);

    g_value_init (&val, G_TYPE_UINT);
    g_value_set_uint (&val, 11);
    g_object_set_property (G_OBJECT (vf), "zoom-level", &val);        
    g_value_set_uint (&val, 8);
    g_object_set_property (G_OBJECT (vf), "zoom-level", &val);        
    g_value_unset (&val);



    g_object_set (G_OBJECT (vf), "autosave-frequency", 0.8888, NULL);        

    viewer_file_open(vf, NULL);    

#if defined(INF_EDITABLE) || defined(INF_EDITABLE_LOSSY)
    viewer_editable_redo(VIEWER_EDITABLE(vf), 1); //主对象转接口
    viewer_editable_undo(VIEWER_EDITABLE(vf), 2);
    g_signal_connect(G_OBJECT(vf), "saved", G_CALLBACK(file_saved), NULL);
#endif //INF_EDITABLE

    viewer_file_write(vf, buf, strlen(buf)+1);

#ifdef INF_LOSSY
    viewer_editable_lossy_compress(VIEWER_EDITABLE_LOSSY(vf));
#elif defined(INF_EDITABLE)
    viewer_editable_save(VIEWER_EDITABLE(vf), NULL);
#endif


    viewer_file_close(vf, NULL);
    
    g_object_unref(vf);
    vf = NULL;

    return 0;
}
